package com.yawn;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * @author Created by yawn on 2017-12-10 11:13
 */
public class TestTaskCompleteAndVariable {

    /**
     * taskService 设置参数时：
     *      setVariable()           全局参数（进行到下一任务task2时，参数还在，可根据task2查询 [完成所有任务后删除]）
     *      serVariableLocal()      本地参数（进行到下一任务task2时，参数不在，无法查询 [完成当前任务后删除]）
     */
    public static void main(String[] args) {

        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        TaskService taskService = processEngine.getTaskService();

        // 部署
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("demo5.bpmn").deploy();
        // 查询流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        // 启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        // 查询当前任务
        Task task1 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        //// 设置参数
        taskService.setVariable(task1.getId(), "days", 5);
        taskService.setVariableLocal(task1.getId(), "name", "yawn");
        //// 获取参数
        Integer days = taskService.getVariable(task1.getId(), "days", Integer.class);
        String name = taskService.getVariableLocal(task1.getId(), "name", String.class);
        System.out.println(name + "请假天数为：" + days);
        /// 在流程定义文件中声明的参数
//        Map<String, Object> processVariables = task1.getProcessVariables();
//        Map<String, Object> taskLocalVariables = task1.getTaskLocalVariables();
        // 完成当前任务
        taskService.complete(task1.getId());
        // 查询当前任务
        Task task2 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        Integer days2 = taskService.getVariable(task2.getId(), "days", Integer.class);
        String name2 = taskService.getVariableLocal(task2.getId(), "name", String.class);
        System.out.println(name2 + "请假天数为：" + days2);
        // 完成当前任务
        taskService.complete(task2.getId());

        processEngine.close();

    }
}
